package org.idea.spring.bean.beandefinitionbuilder;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @Author linhao
 * @Date created in 3:54 下午 2021/5/17
 */
public class GenericBeanDefinitionDemo {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.register(GenericBeanDefinitionDemo.class);
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) annotationConfigApplicationContext.getBeanFactory();
        //通过结合beanfactory的帮忙，可以手动定制一些beanDefinition 然后注册到spring容器中，这样Spring容器在后续获取bean的时候也会顺带初始化
        GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        genericBeanDefinition.setBeanClass(User.class);
        defaultListableBeanFactory.registerBeanDefinition("user",genericBeanDefinition);
        annotationConfigApplicationContext.refresh();
        User user = (User) annotationConfigApplicationContext.getBean("user");
        System.out.println(user.toString());
        annotationConfigApplicationContext.close();
    }
}
